package newBee.class4;

import cn.hutool.core.util.ArrayUtil;
import common.ArrayUtils;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 最多开会问题
 * 给定一组会议，每个会议都有开始时间和结束时间，选择最多的会议集合，使得它们之间没有时间重叠。
 */
public class Code03_maxMeetings {
    public static void main(String[] args) {
        int[][] meetings = {{1, 4}, {2, 3}, {3, 5}, {5, 6}, {7, 8}};
        int i = maxMeetings(meetings);
        System.out.println("i = " + i);
    }

    public static int maxMeetings(int[][] arr) {
        Meeting[] meetings = new Meeting[arr.length];
        int i = 0;
        for (int[] m : arr) {
            meetings[i++] = new Meeting(m[0], m[1]);
        }
        Arrays.sort(meetings, (e1, e2) -> e1.end - e2.end);
        int timeLine = 0;
        int ans = 0;
        for (int j = 0; j < meetings.length; j++) {
            if (timeLine <= meetings[j].start) { // 如果当前会议的开始时间 >= 上一个会的结束时间
                ans++;
                timeLine = meetings[j].end; // 时间点来到end
            }
        }
        return ans;
    }

    private static class Meeting {
        public int start;
        public int end;

        public Meeting(int start, int end) {
            this.start = start;
            this.end = end;
        }

        @Override
        public String toString() {
            return "Meeting{" +
                    "start=" + start +
                    ", end=" + end +
                    '}';
        }
    }
}
